<?php
if ($do === 'addCat') {
   $addCatQuery = "INSERT INTO `category` SET `category`='".mysql_real_escape_string($_REQUEST['catName'])."',`filename`='".mysql_real_escape_string($_REQUEST['catFilename'])."' ";
   if ($_REQUEST['catDefault']) $addCatQuery .= " ,`default`='".$_REQUEST['catDefault']."' ";
   mysql_query($addCatQuery);
} else if ($do === 'editCat') {
   if ($_REQUEST['cat']) {
      foreach ($_REQUEST['cat'] as $cat) {
         if ( ($cat[edit]) && ($_REQUEST['catEditDo'] === 'del') ) {
            mysql_query(" DELETE FROM `category` WHERE `id`='".$cat['id']."' ");
         } else {
            $updCatQuery = " UPDATE `category` SET `category`='".mysql_real_escape_string($cat[name])."',`filename`='".mysql_real_escape_string($cat[filename])."',`prefix`='".mysql_real_escape_string($cat[prefix])."',`default`=";
            if ($_REQUEST['catDefault'] == $cat[id]) $updCatQuery .= "'1' ";
            else $updCatQuery .= "'0' ";
            $updCatQuery .= " WHERE `id`='".$cat[id]."' ";
            mysql_query ($updCatQuery);
         }
      }
   }
}
?>

<form action="?mode=category&do=addCat" method="post">
<fieldset><legend>Add Category</legend>
<table>
<tr><td>Name</td><td><input type="text" name="catName" onInput="var filename = this.value.replace(/\W/g, ''); updCatFilename(filename)" /></td></tr>
<tr><td>Filename</td><td><span id="catFilename"><input type="text" name="catFilename" /></span>(no spaces/funky characters)</td></tr>
<tr><td>Default</td><td><input type="checkbox" value="1" name="catDefault" /></td></tr>
</table>
<input type="submit" value="Add Category" />
</fieldset>
</form>
<br />
<form action="?mode=category&do=editCat" method="post"><fieldset><legend>Edit Categories</legend><table>
<tr>
<td></td><td>Category name</td><td>abbrev. name</td><td>prefix</td><td>default</td>
</tr>
<?php
$i=0;
$catQuery = mysql_query("SELECT * FROM `category` ");
while ($cat = mysql_fetch_array($catQuery)) {
   ?>
   <tr>
   <td><input type="hidden" name="cat[<?=$i?>][id]" value="<?=$cat[id]?>" /><input type="checkbox" name="cat[<?=$i?>][edit]" value="<?=$cat[id]?>" /></td>
   <td><input type="text" name="cat[<?=$i?>][name]" value="<?php echo($cat[category]) ?>" /></td>
   <td><input type="text" name="cat[<?=$i?>][filename]" value="<?php echo($cat[filename]) ?>" /></td>
   <td><input type="text" size="1" name="cat[<?=$i?>][prefix]" value="<?php echo($cat[prefix]) ?>" /></td>
   <td><input type="radio" name="catDefault" value="<?=$cat[id]?>" <?php if ($cat['default'] == 1) echo ('checked="checked"') ?> /></td>
   </tr>
   <?php 
   $i++;
   unset ($cat);
}
?>
</table>
<select name="catEditDo">
   <option value="" selected="selected">With Selected:</option>
   <option value="del">Delete</option>
</select>
<input type="submit" value="Save Changes" />
</fieldset>
</form>
